

import { Body, Controller, Get,Post } from '@nestjs/common';
import { AllNewAndBenfitService } from 'src/services/all-new-and-benfit/all-new-and-benfit.service';
import { IAllNewAndBenfit } from 'src/types/allNewAndBenfit.interface';
import HttpResponse from 'src/types/http.response.interface';

type UnionType = IAllNewAndBenfit[] | IAllNewAndBenfit;


@Controller('allnewandbenfit')
export class AllNewAndBenfitController {
      //等价于上面的写法
      constructor(private readonly  allNewAndBenfitService:AllNewAndBenfitService){

      }

      @Get('/findAll')  
      async findAll():Promise<HttpResponse<IAllNewAndBenfit[]>>{
         const data = await this.allNewAndBenfitService.findService();
         if(Array.isArray(data)){
          return {
              success:true,
              message:"查询数据成功",
              data,
          }
         }
         else{
          return {
              success:false,
              message:"查询数据失败",
              data:[],
          }
         }
         
      }
      /**
     * 用来通过postman或者insomnia来导入数据
     * 在浏览器中输入http://localhost:9898/allnewandbenfit/importData
     */
    @Post('/importData')
    //DTO Data Transfer Object 数据传输对象，当客户端把数据传送到后台的
    async create(@Body() requestParamsBody:UnionType) {

        const data:UnionType = await this.allNewAndBenfitService.importData(requestParamsBody);
        return {
            success:true,
            message:"数据导入成功",
            data
        };
    }
  
}
